package light.head.framework.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @author janhang
 * @2011-9-20  上午08:42:55
 */
public class StringUtils {
	
	private final static char[] str = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private final static char[] no = "123456789".toCharArray();
	
	/**
	 * 判断是否GB2312
	 * @param 要判断的字符
	 * @return 是否GB2312编码
	 * @author 简航
	 * 创建日期 2009-08-19 13:17:19
	 */
	public static boolean isGB2312(char c) {
		byte[] bytes = null;
		try {
			bytes = (String.valueOf(c)).getBytes("UTF8");
			if (bytes.length > 1) {
				bytes = (String.valueOf(c)).getBytes("GB2312");
				if (bytes.length == 1)
					return false;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 字符串数组转整型数组
	 * @param	要转换的字符串数组
	 * @return	转换后的整型数组
	 */
	public static Integer[] sa2ia(String[] sa) {
		if (sa == null || 0 == sa.length)
			return null;
		
		Integer[] ia = new Integer[sa.length];
		
		for (int i = 0; i < sa.length; i ++) {
			ia[i] = Integer.parseInt(sa[i]);
		}
		return ia;
	}
	
	/**
	 * 随机生成指定长度的字符串
	 * @param	指定长度
	 */
	public static String randStr(int len) {
		if (len < 1) {
			return null;
		}
		Random rand = new Random();
		char[] result = new char[len];
		for (int i = 0; i < len; i++)
			result[i] = str[rand.nextInt(str.length)];
		
		return new String(result);
	}
	
	/**
	 * 随机生成指定长度的数字
	 * @param	指定长度
	 */
	public static String randNo(int len) {
		if (len < 1) {
			return null;
		}
		Random rand = new Random();
		char[] result = new char[len];
		for (int i = 0; i < len; i++)
			result[i] = no[rand.nextInt(no.length)];
		
		return new String(result);
	}
}
